﻿
#
# Author : itech
# 

import os
import sys
import shutil

def copyWithRename(source, dest, rename = True):

  newdest = dest
  if os.path.exists(dest) and rename == True:
    dir, name = os.path.split(dest)
    if os.path.isfile(dest):
      namewithoutext, ext = os.path.splitext(name)
      i = 1
      while(1):
        newdest = os.path.join(dir, namewithoutext + '.R' + str(i) + ext)
        if os.path.exists(newdest):
          i+=1
          continue
        else: break
    else:
      i = 1
      while(1):
        newdest = os.path.join(dir, name + '.R' + str(i))
        if os.path.exists(newdest):
          i+=1
          continue
        else: break
    
  print source + 'will be renamed and copied to ' + newdest

  if os.path.isdir(source):
    shutil.copytree(source,newdest)
  else:
    shutil.copyfile(source,newdest)
    

def usage():
  print '''
  Usage:
    python CopyWithAutoRename.py source dest
  Eexamples:
    python CopyWithAutoRename.py "c:\\test\\test.txt" "c:\\test2\\test.txt"
    python CopyWithAutoRename.py "c:\\test\\test1" "c:\\test\\test2"
  '''

if __name__ == '__main__':
  if len(sys.argv) == 3: 
    copyWithRename(sys.argv[1], sys.argv[2])
  else:
    usage()



